home *** CD-ROM | disk | FTP | other *** search
/ Champak 66 / Vol 66.iso / games / doeo.swf / scripts / __Packages / NewgroundsAPI.as
Encoding:
Text File  |  2013-04-24  |  15.2 KB  |  372 lines

  1. class NewgroundsAPI
  2. {
  3.    var firstChild;
  4.    static var tracker_id;
  5.    static var host;
  6.    static var version;
  7.    static var debug;
  8.    static var error_format;
  9.    static var header_format;
  10.    static var normal_format;
  11.    static var link_format;
  12.    static var movie_options = new Object();
  13.    static var custom_events = new Object();
  14.    static var custom_links = new Object();
  15.    static var MOVIE_VIEWS = 1;
  16.    static var AUTHOR_SITE = 2;
  17.    static var NEWGROUNDS = 3;
  18.    static var NEW_VERSION = 4;
  19.    static var CUSTOM_STATS = 50;
  20.    static var GATEWAY_URL = "http://www.ngads.com/gateway.php";
  21.    function NewgroundsAPI()
  22.    {
  23.    }
  24.    static function connectMovie(id)
  25.    {
  26.       if(!id)
  27.       {
  28.          NewgroundsAPI.SendError("Missing required \'id\' parameter in NewgroundsAPI.connectMovie(id:Number)");
  29.       }
  30.       else if(!NewgroundsAPI.tracker_id)
  31.       {
  32.          NewgroundsAPI.SendMessage("Connecting to API gateway...");
  33.          NewgroundsAPI.tracker_id = id;
  34.          NewgroundsAPI.host = _url.split("/")[2].toLowerCase();
  35.          if(NewgroundsAPI.host.length < 1)
  36.          {
  37.             NewgroundsAPI.host = "localhost";
  38.          }
  39.          var _loc2_ = new Object();
  40.          NewgroundsAPI.SendEvent(NewgroundsAPI.MOVIE_VIEWS);
  41.       }
  42.    }
  43.    static function setMovieVersion(movie_version)
  44.    {
  45.       if(!movie_version)
  46.       {
  47.          NewgroundsAPI.SendError("Missing required \'version\' in NewgroundsAPI.setMovieVersion(version:String)");
  48.       }
  49.       else
  50.       {
  51.          NewgroundsAPI.version = movie_version;
  52.       }
  53.    }
  54.    static function debugMode()
  55.    {
  56.       NewgroundsAPI.debug = true;
  57.    }
  58.    static function addCustomEvent(stat_id, stat_name)
  59.    {
  60.       if(!stat_id)
  61.       {
  62.          NewgroundsAPI.SendError("Missing required \'id\' parameter in NewgroundsAPI.AddCustomEvent(id:Number, event_name:String)");
  63.       }
  64.       else if(!stat_name)
  65.       {
  66.          NewgroundsAPI.SendError("Missing required \'event_name\' parameter in NewgroundsAPI.AddCustomEvent(id:Number, event_name:String)");
  67.       }
  68.       else
  69.       {
  70.          NewgroundsAPI.custom_events[stat_name] = NewgroundsAPI.CUSTOM_STATS + stat_id;
  71.          NewgroundsAPI.SendMessage("Created custom event: " + stat_name);
  72.       }
  73.    }
  74.    static function addCustomLink(stat_id, stat_name)
  75.    {
  76.       if(!stat_id)
  77.       {
  78.          NewgroundsAPI.SendError("Missing required \'id\' parameter in NewgroundsAPI.AddCustomLink(id:Number, link_name:String)");
  79.       }
  80.       else if(!stat_name)
  81.       {
  82.          NewgroundsAPI.SendError("Missing required \'link_name\' parameter in NewgroundsAPI.AddCustomLink(id:Number, link_name:String)");
  83.       }
  84.       else
  85.       {
  86.          NewgroundsAPI.custom_links[stat_name] = NewgroundsAPI.CUSTOM_STATS + stat_id;
  87.          NewgroundsAPI.SendMessage("Created custom link " + stat_id + ": " + stat_name);
  88.       }
  89.    }
  90.    static function loadMySite()
  91.    {
  92.       NewgroundsAPI.SendLink(NewgroundsAPI.AUTHOR_SITE);
  93.    }
  94.    static function loadNewgrounds()
  95.    {
  96.       NewgroundsAPI.SendLink(NewgroundsAPI.NEWGROUNDS);
  97.    }
  98.    static function logCustomEvent(event_name)
  99.    {
  100.       if(!event_name)
  101.       {
  102.          NewgroundsAPI.SendError("Missing required \'event_name\' parameter in NewgroundsAPI.logCustomEvent(event_name:String)");
  103.       }
  104.       else if(!NewgroundsAPI.custom_events[event_name])
  105.       {
  106.          NewgroundsAPI.SendError("Attempted to log undefined custom event: " + event_name);
  107.       }
  108.       else
  109.       {
  110.          NewgroundsAPI.SendEvent(NewgroundsAPI.custom_events[event_name]);
  111.       }
  112.    }
  113.    static function loadCustomLink(link_name)
  114.    {
  115.       if(!link_name)
  116.       {
  117.          NewgroundsAPI.SendError("Missing required \'link_name\' parameter in NewgroundsAPI.loadCustomLink(link_name:String)");
  118.       }
  119.       else if(!NewgroundsAPI.custom_links[link_name])
  120.       {
  121.          NewgroundsAPI.SendError("Attempted to open undefined custom link: " + link_name);
  122.       }
  123.       else
  124.       {
  125.          NewgroundsAPI.SendLink(NewgroundsAPI.custom_links[link_name]);
  126.       }
  127.    }
  128.    static function getAdURL()
  129.    {
  130.       return NewgroundsAPI.movie_options.ad_url;
  131.    }
  132.    static function getMovieURL()
  133.    {
  134.       if(NewgroundsAPI.movie_options.movie_url)
  135.       {
  136.          return NewgroundsAPI.movie_options.movie_url;
  137.       }
  138.       return "Newgrounds.com";
  139.    }
  140.    static function getNewVersionURL()
  141.    {
  142.       return NewgroundsAPI.GATEWAY_URL + "?&id=" + NewgroundsAPI.tracker_id + "&host=" + escape(NewgroundsAPI.host) + "&stat=" + NewgroundsAPI.NEW_VERSION;
  143.    }
  144.    static function SendEvent(id)
  145.    {
  146.       NewgroundsAPI.SendStat(id,false);
  147.    }
  148.    static function SendLink(id)
  149.    {
  150.       NewgroundsAPI.SendStat(id,true);
  151.    }
  152.    static function ReadGatewayData(params)
  153.    {
  154.       for(var _loc2_ in params)
  155.       {
  156.          params[_loc2_] = unescape(params[_loc2_]);
  157.          NewgroundsAPI.movie_options[_loc2_] = params[_loc2_];
  158.       }
  159.       if(params.settings_loaded)
  160.       {
  161.          NewgroundsAPI.SendMessage("You have successfully connected to the Newgrounds API gateway!");
  162.          NewgroundsAPI.SendMessage("Movie Identified as \'" + NewgroundsAPI.movie_options.movie_name + "\'");
  163.          if(NewgroundsAPI.movie_options.message)
  164.          {
  165.             NewgroundsAPI.SendMessage(NewgroundsAPI.movie_options.message);
  166.          }
  167.          if(NewgroundsAPI.movie_options.ad_url)
  168.          {
  169.             NewgroundsAPI.SendMessage("Your movie has been approved to run Flash Ads");
  170.             NewgroundsAPI.onAdsApproved(NewgroundsAPI.movie_options.ad_url);
  171.          }
  172.          if(NewgroundsAPI.movie_options.movie_version and NewgroundsAPI.movie_options.movie_version.toString() != NewgroundsAPI.version.toString())
  173.          {
  174.             NewgroundsAPI.SendMessage("WARNING: The movie version configured in your API settings does not match this movie\'s version!");
  175.             NewgroundsAPI.onNewVersionAvailable(NewgroundsAPI.movie_options.movie_version,NewgroundsAPI.getMovieURL(),NewgroundsAPI.getNewVersionURL());
  176.          }
  177.          if(NewgroundsAPI.movie_options.deny_host)
  178.          {
  179.             NewgroundsAPI.SendMessage("You have blocked \'localHost\' in your API settings.");
  180.             NewgroundsAPI.SendMessage("If you wish to test your movie you will need to remove this block.");
  181.             NewgroundsAPI.onDenyHost(NewgroundsAPI.host,NewgroundsAPI.getMovieURL(),NewgroundsAPI.getNewVersionURL());
  182.          }
  183.          if(NewgroundsAPI.movie_options.request_portal_url == 1)
  184.          {
  185.             var _loc4_ = NewgroundsAPI.GATEWAY_URL + "?&id=" + NewgroundsAPI.tracker_id + "&portal_url=" + escape(_url);
  186.             var _loc3_ = new XML();
  187.             _loc3_.ignoreWhite = true;
  188.             _loc3_.load(_loc4_);
  189.          }
  190.       }
  191.       else if(!NewgroundsAPI.movie_options.settings_loaded)
  192.       {
  193.          NewgroundsAPI.SendError("Could not establish connection to the API gateway.");
  194.       }
  195.    }
  196.    static function SendStat(stat_id, open_in_browser)
  197.    {
  198.       if(!NewgroundsAPI.tracker_id)
  199.       {
  200.          NewgroundsAPI.SendError("API calls cannot be made without a valid movie id");
  201.       }
  202.       else
  203.       {
  204.          var _loc7_ = NewgroundsAPI.GATEWAY_URL + "?&id=" + NewgroundsAPI.tracker_id + "&host=" + escape(NewgroundsAPI.host) + "&stat=" + stat_id;
  205.          if(NewgroundsAPI.debug)
  206.          {
  207.             _loc7_ += "&debug=1";
  208.          }
  209.          if(open_in_browser)
  210.          {
  211.             getURL(_loc7_,"_blank");
  212.          }
  213.          else
  214.          {
  215.             var _loc8_ = new XML();
  216.             _loc8_.ignoreWhite = true;
  217.             _loc8_.onLoad = function(success)
  218.             {
  219.                var _loc6_ = new Object();
  220.                var _loc3_ = 0;
  221.                while(_loc3_ < this.firstChild.childNodes.length)
  222.                {
  223.                   var _loc4_ = this.firstChild.childNodes[_loc3_];
  224.                   var _loc5_ = _loc4_.nodeName;
  225.                   var _loc2_ = _loc4_.attributes.value;
  226.                   if(_loc2_ == Number(_loc2_))
  227.                   {
  228.                      _loc2_ = Number(_loc2_);
  229.                   }
  230.                   _loc6_[_loc5_] = _loc2_;
  231.                   _loc3_ = _loc3_ + 1;
  232.                }
  233.                NewgroundsAPI.ReadGatewayData(_loc6_);
  234.             };
  235.             _loc8_.load(_loc7_);
  236.          }
  237.       }
  238.    }
  239.    static function SendError(msg)
  240.    {
  241.       trace("[NEWGROUNDS API ERROR] :: " + msg);
  242.    }
  243.    static function SendMessage(msg)
  244.    {
  245.       trace("[NEWGROUNDS API] :: " + msg);
  246.    }
  247.    static function InitTextFormats()
  248.    {
  249.       if(!NewgroundsAPI.error_format)
  250.       {
  251.          NewgroundsAPI.error_format = new TextFormat();
  252.          NewgroundsAPI.error_format.font = "Arial Black";
  253.          NewgroundsAPI.error_format.size = 48;
  254.          NewgroundsAPI.error_format.color = 16711680;
  255.       }
  256.       if(!NewgroundsAPI.header_format)
  257.       {
  258.          NewgroundsAPI.header_format = new TextFormat();
  259.          NewgroundsAPI.header_format.font = "Arial Black";
  260.          NewgroundsAPI.header_format.size = 24;
  261.          NewgroundsAPI.header_format.color = 16777215;
  262.       }
  263.       if(!NewgroundsAPI.normal_format)
  264.       {
  265.          NewgroundsAPI.normal_format = new TextFormat();
  266.          NewgroundsAPI.normal_format.font = "Arial";
  267.          NewgroundsAPI.normal_format.bold = true;
  268.          NewgroundsAPI.normal_format.size = 12;
  269.          NewgroundsAPI.normal_format.color = 16777215;
  270.       }
  271.       if(!NewgroundsAPI.link_format)
  272.       {
  273.          NewgroundsAPI.link_format = new TextFormat();
  274.          NewgroundsAPI.link_format.color = 16776960;
  275.          NewgroundsAPI.link_format.underline = true;
  276.       }
  277.    }
  278.    static function onNewVersionAvailable(version, movie_url, redirect_url)
  279.    {
  280.       NewgroundsAPI.InitTextFormats();
  281.       var _loc2_ = new Object();
  282.       _loc2_.x = Stage.width / 2;
  283.       _loc2_.y = Stage.height / 2;
  284.       _root.createEmptyMovieClip("NGAPI_new_version_overlay",_root.getNextHighestDepth());
  285.       _root.NGAPI_new_version_overlay.lineStyle(1,0,100);
  286.       _root.NGAPI_new_version_overlay.beginFill(0,70);
  287.       _root.NGAPI_new_version_overlay.moveTo(-10,-10);
  288.       _root.NGAPI_new_version_overlay.lineTo(-10,1000);
  289.       _root.NGAPI_new_version_overlay.lineTo(1000,1000);
  290.       _root.NGAPI_new_version_overlay.lineTo(1000,-10);
  291.       _root.NGAPI_new_version_overlay.lineTo(-10,-10);
  292.       _root.NGAPI_new_version_overlay.endFill();
  293.       _root.NGAPI_new_version_overlay.lineStyle(10,0,100);
  294.       _root.NGAPI_new_version_overlay.beginFill(51);
  295.       _root.NGAPI_new_version_overlay.moveTo(_loc2_.x - 240,_loc2_.y - 120);
  296.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x + 240,_loc2_.y - 120);
  297.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x + 240,_loc2_.y + 80);
  298.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x - 240,_loc2_.y + 80);
  299.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x - 240,_loc2_.y - 120);
  300.       _root.NGAPI_new_version_overlay.endFill();
  301.       _root.NGAPI_new_version_overlay.createEmptyMovieClip("exit",1000);
  302.       _root.NGAPI_new_version_overlay.exit.lineStyle(2,39423,100);
  303.       _root.NGAPI_new_version_overlay.exit.beginFill(0,50);
  304.       _root.NGAPI_new_version_overlay.exit.moveTo(_loc2_.x + 210,_loc2_.y - 110);
  305.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 230,_loc2_.y - 110);
  306.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 230,_loc2_.y - 90);
  307.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 210,_loc2_.y - 90);
  308.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 210,_loc2_.y - 110);
  309.       _root.NGAPI_new_version_overlay.exit.endFill();
  310.       _root.NGAPI_new_version_overlay.exit.moveTo(_loc2_.x + 214,_loc2_.y - 106);
  311.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 226,_loc2_.y - 94);
  312.       _root.NGAPI_new_version_overlay.exit.moveTo(_loc2_.x + 226,_loc2_.y - 106);
  313.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 214,_loc2_.y - 94);
  314.       _root.NGAPI_new_version_overlay.exit.onMouseUp = function()
  315.       {
  316.          if(_root.NGAPI_new_version_overlay.exit.hitTest(_root._xmouse,_root._ymouse))
  317.          {
  318.             _root.NGAPI_new_version_overlay.removeMovieClip();
  319.          }
  320.       };
  321.       var _loc3_ = "Version " + version + " is now available at:" + "\n";
  322.       var _loc5_ = _loc3_.length;
  323.       _loc3_ += movie_url;
  324.       var _loc4_ = _loc3_.length;
  325.       _root.NGAPI_new_version_overlay.createTextField("mouseblocker",99,-10,-10,1000,1000);
  326.       _root.NGAPI_new_version_overlay.createTextField("newversion",100,_loc2_.x - 210,_loc2_.y - 90,400,80);
  327.       _root.NGAPI_new_version_overlay.newversion.text = "New Version Available!";
  328.       _root.NGAPI_new_version_overlay.newversion.setTextFormat(NewgroundsAPI.header_format);
  329.       _root.NGAPI_new_version_overlay.createTextField("message",101,(Stage.width - 400) / 2,Stage.height / 2,400,40);
  330.       _root.NGAPI_new_version_overlay.message.text = _loc3_;
  331.       _root.NGAPI_new_version_overlay.message.multiline = true;
  332.       _root.NGAPI_new_version_overlay.message.wordWrap = true;
  333.       _root.NGAPI_new_version_overlay.message.html = true;
  334.       _root.NGAPI_new_version_overlay.message.setTextFormat(NewgroundsAPI.normal_format);
  335.       NewgroundsAPI.link_format.url = redirect_url;
  336.       _root.NGAPI_new_version_overlay.message.setTextFormat(_loc5_,_loc4_,NewgroundsAPI.link_format);
  337.    }
  338.    static function onDenyHost(hostname, movie_url, redirect_url)
  339.    {
  340.       NewgroundsAPI.InitTextFormats();
  341.       _root.createEmptyMovieClip("NGAPI_deny_host_overlay",_root.getNextHighestDepth());
  342.       _root.NGAPI_deny_host_overlay.lineStyle(20,0,100);
  343.       _root.NGAPI_deny_host_overlay.beginFill(6684672);
  344.       _root.NGAPI_deny_host_overlay.moveTo(0,0);
  345.       _root.NGAPI_deny_host_overlay.lineTo(Stage.width,0);
  346.       _root.NGAPI_deny_host_overlay.lineTo(Stage.width,Stage.height);
  347.       _root.NGAPI_deny_host_overlay.lineTo(0,Stage.height);
  348.       _root.NGAPI_deny_host_overlay.lineTo(0,0);
  349.       _root.NGAPI_deny_host_overlay.endFill();
  350.       var _loc2_ = "This movie has not been approved for use on " + hostname + ".";
  351.       _loc2_ += "\r\rFor an aproved copy, please visit:\r";
  352.       var _loc4_ = _loc2_.length;
  353.       _loc2_ += movie_url;
  354.       var _loc3_ = _loc2_.length;
  355.       _root.NGAPI_deny_host_overlay.createTextField("mousekill",100,0,0,Stage.width,Stage.height);
  356.       _root.NGAPI_deny_host_overlay.createTextField("error",101,(Stage.width - 400) / 2,Stage.height / 2 - 100,400,200);
  357.       _root.NGAPI_deny_host_overlay.error.text = "ERROR!";
  358.       _root.NGAPI_deny_host_overlay.error.setTextFormat(NewgroundsAPI.error_format);
  359.       _root.NGAPI_deny_host_overlay.createTextField("message",102,(Stage.width - 400) / 2,Stage.height / 2,400,200);
  360.       _root.NGAPI_deny_host_overlay.message.text = _loc2_;
  361.       _root.NGAPI_deny_host_overlay.message.multiline = true;
  362.       _root.NGAPI_deny_host_overlay.message.wordWrap = true;
  363.       _root.NGAPI_deny_host_overlay.message.html = true;
  364.       _root.NGAPI_deny_host_overlay.message.setTextFormat(NewgroundsAPI.normal_format);
  365.       NewgroundsAPI.link_format.url = redirect_url;
  366.       _root.NGAPI_deny_host_overlay.message.setTextFormat(_loc4_,_loc3_,NewgroundsAPI.link_format);
  367.    }
  368.    static function onAdsApproved(ad_url)
  369.    {
  370.    }
  371. }
  372.